package com.bwie.bwedu.presenter.impl;

import android.text.TextUtils;

import com.bwie.bwedu.activity.LoginActivity;
import com.bwie.bwedu.model.ILoginModel;
import com.bwie.bwedu.model.impl.LoginModelImpl;
import com.bwie.bwedu.net.HttpUtils;
import com.bwie.bwedu.presenter.ILoginPresenter;
import com.bwie.bwedu.utils.ASimpleCacheUtils;
import com.bwie.bwedu.utils.PhoneUtil;

/**
 * Created by Administrator on 2016/7/22.
 */
public class LoginPresenterImpl implements ILoginPresenter,ILoginModel.LoginListener {
    private LoginActivity activity;
    private final LoginModelImpl model;
    private boolean single=true;

    public LoginPresenterImpl(LoginActivity activity) {
        this.activity = activity;
        model = new LoginModelImpl();
    }

    @Override
    public void login(String username, String pwd) {
        if (TextUtils.isEmpty(username)||TextUtils.isEmpty(pwd)){
            activity.onFail("用户名或密码不能为空！");
            return;
        }else{
            if(!PhoneUtil.isMobileNO(username)){
                activity.onFail("手机号码不合法！");
                return;
            }else{
                if (!HttpUtils.isNetworkConnected()){
                    activity.onFail("请检查您的网络状态！");
                    return;
                }
            }
        }

        if (single){
            ASimpleCacheUtils.write("password",pwd);
            model.login(username,pwd,this);
            single=false;
        }

    }

    @Override
    public void onLoginSuccess() {
        activity.onSuccess();
        single=true;
    }

    @Override
    public void onLoginFail(String info) {
        activity.onFail(""+info);
        single=true;
    }
}
